package com.hdax.dm.controller;

import com.hdax.dm.entity.pay.DmTrade;
import com.hdax.dm.feign.PayTradeFeign;
import com.hdax.dm.feign.SeatFeign;
import com.hdax.dm.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/order")
public class OrderPayController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private SeatFeign seatFeign;

    @Autowired
    private PayTradeFeign payTradeFeign;

    @RequestMapping("payed")
    public void  changeOrder(HttpServletRequest request){
        //商家订单号
        String orderNo=request.getParameter("out_trade_no");
        //交易订单号
        String trade_no=request.getParameter("trade_no");
        //交易金额
        Double total_amount= Double.valueOf(request.getParameter("receipt_amount"));

        Boolean i= orderService.orderUpdate(2L,orderNo);
        if (i){
            seatFeign.ishaveSeatUpdate(3L,orderNo);
            System.out.println("支付成功");


            DmTrade trade=new DmTrade();
            trade.setId(orderNo);
            trade.setOrderNo(orderNo);
            trade.setTradeNo(trade_no);
            trade.setPayMethod(1L);
            trade.setAmount(total_amount);
            trade.setCreatedTime(LocalDateTime.now());
            payTradeFeign.payTrade(trade);
        }

        System.out.println("支付宝订单号："+request.getParameter("trade_no"));
        System.out.println("商家订单号："+request.getParameter("out_trade_no"));
        System.out.println("买家支付宝id："+request.getParameter("buyer_id"));
        System.out.println("交易状态："+request.getParameter("trade_status"));
        System.out.println("实收金额："+request.getParameter("receipt_amount"));
        System.out.println("用户支付金额："+request.getParameter("buyer_pay_amount"));
        System.out.println("交易付款时间："+request.getParameter("gmt_payment"));

    }
}
